package idea.think;


import idea.think.db.IT_sql;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class IdeaAdd extends Activity{			
	final static int DIALOG_POST_STATUS = 1;
	final static int DIALOG_SOFT_KEY = 2;
	final static int DIALOG_DRAW = 3;	
	int check;
	Spinner mSpin;
	ImageView mItem01, mItem02, mItem03;
	EditText mEdit;
	Button softKey;
	ImageView save, nateSch;
	TextView mTextItem01, mTextItem02, mTextItem03;
	private IT_sql sql;	
	SQLiteDatabase db;
	Cursor cursor;
	int spinCheck = 0;
	ArrayList<String> nateSearch = new ArrayList<String>();
	ArrayList<String> aListCategory = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_idea);		
		sql = new IT_sql(this);		
		mTextItem01 = (TextView) findViewById(R.id.textItem01);
		mTextItem02 = (TextView) findViewById(R.id.textItem02);
		mTextItem03 = (TextView) findViewById(R.id.textItem03);		
		mItem01 = (ImageView) findViewById(R.id.addItem01);
		mItem02 = (ImageView) findViewById(R.id.addItem02);
		mItem03 = (ImageView) findViewById(R.id.addItem03);
		save = (ImageView) findViewById(R.id.saveBtn);
		nateSch = (ImageView) findViewById(R.id.nateBtn);		
		mSpin = (Spinner) findViewById(R.id.addSpin);		
		db = sql.getWritableDatabase();
		cursor = selectCategory(db);
		showCategory(cursor);
		
		String [] from = new String[]{"catename"};
		int [] to = new int[]{android.R.id.text1};
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
				android.R.layout.simple_spinner_item, cursor, from, to);				
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		mSpin.setAdapter(adapter);
		
		mSpin.setOnItemSelectedListener(new OnItemSelectedListener() {            
	         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {                
	        	 Log.i("TA", "onItemPosition = " + aListCategory.get(arg2));
	        	 spinCheck = arg2;	        	 
	         } 
	         public void onNothingSelected(AdapterView<?> arg0) {}        
        });
	
		mItem01.setOnClickListener(new View.OnClickListener() { //idea1			
			
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_SOFT_KEY);
				check = 1;				
			}
		});	
		
		mItem02.setOnClickListener(new View.OnClickListener() { //idea2			
			
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_SOFT_KEY);
				check = 2;
			}
		});
		
		mItem03.setOnClickListener(new View.OnClickListener() { //idea3			
			
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_SOFT_KEY);
				check = 3;
			}
		});
		
		save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(!mTextItem02.getText().toString().equals("")	&& !mTextItem03.getText().toString().equals("")){
					nateSearch.add(mTextItem01.getText().toString());
					nateSearch.add(mTextItem02.getText().toString());
					nateSearch.add(mTextItem03.getText().toString());
//					addItem(db, nateSearch.get(0).toString(), nateSearch.get(1).toString(), nateSearch.get(2).toString(), spinCheck);
					addItem(db, mTextItem01.getText().toString(), mTextItem02.getText().toString(), mTextItem03.getText().toString(), spinCheck);
															
					mTextItem01.setText("");
					mTextItem02.setText("");
					mTextItem03.setText("");
					
				}else{
					Toast.makeText(IdeaAdd.this, "아이디어가 부족합니다.", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		nateSch.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(nateSearch.size() == 3){
					Intent intent = new Intent(IdeaAdd.this, NateWeb.class);
					intent.putExtra("ITEM1", nateSearch.get(0).toString());
					intent.putExtra("ITEM2", nateSearch.get(1).toString());
					intent.putExtra("ITEM3", nateSearch.get(2).toString());
					nateSearch.clear();
					startActivity(intent);
				}else{
					Toast.makeText(IdeaAdd.this, "저장이 안됬거나 idea 저장 개수가 부족합니다.", Toast.LENGTH_SHORT).show();
				}
			}
		});		
	}
			
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		switch(id) {
		case DIALOG_POST_STATUS :{					
			builder.setTitle("입력방식");
			builder.setMessage("입력방식을 선택하세요");
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.select_input, null);
			builder.setView(layout);			
			softKey = (Button) layout.findViewById(R.id.softkey);
			
			softKey.setOnClickListener(new View.OnClickListener() { //SoftKEY
				
				@Override
				public void onClick(View v) {					
					showDialog(DIALOG_SOFT_KEY);
					dismissDialog(DIALOG_POST_STATUS);
				}
			});
			
			return builder.create();
		}
		
		case DIALOG_SOFT_KEY :{						
			//builder.setTitle("SoftKey");			
			builder.setMessage("아이디어를 입력하세요");
			mEdit = (EditText)getLayoutInflater().inflate(R.layout.dialogedit, null);
			builder.setView(mEdit);			
			builder.setPositiveButton("입력", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {					
					
					String ideaAdd = mEdit.getText().toString();
					
					if(ideaAdd == "" || ideaAdd.equals("") || ideaAdd ==  null){
						Toast.makeText(IdeaAdd.this, "아무것도 입력 되지 않았습니다.", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(IdeaAdd.this, ideaAdd + " 추가", Toast.LENGTH_SHORT).show();
						if(check == 1){
							mTextItem01.setText(ideaAdd);
							mEdit.setText("");
						}else if(check == 2){
							mTextItem02.setText(ideaAdd);
							mEdit.setText("");
						}else{
							mTextItem03.setText(ideaAdd);
							mEdit.setText("");
						}
					}
				}
			});	
									
			builder.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					//code
					dismissDialog(DIALOG_SOFT_KEY);
				}
			});
			
			return builder.create();
		}	
		}
		return super.onCreateDialog(id);
	}
		
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {		
		super.onPrepareDialog(id, dialog);
		mEdit.setText("");
	}
	
	//idea insert
	public void addItem(SQLiteDatabase db, String item1, String item2, String item3, int spinCheck){
		Cursor c = selectCategory(db);
		showCategory(c);
		
		ContentValues values = new ContentValues();
		
		if(aListCategory.size() != 0){
			values.put("catename", aListCategory.get(spinCheck));
			values.put("idea1", item1);
			values.put("idea2", item2);
			values.put("idea3", item3);
			values.put("result", item1 + " " + item2 + " " + item3);
			db.insertOrThrow(IT_sql.IDEA_TBL, null, values);			
			Toast.makeText(IdeaAdd.this, "저장완료, 저장된 idea를 검색해보세요", Toast.LENGTH_SHORT).show();
			
		}else{
			Toast.makeText(IdeaAdd.this, "저장 할 수 없습니다. : 카테고리 없음", Toast.LENGTH_SHORT).show();
		}
	}
	
	public Cursor selectCategory(SQLiteDatabase db){
		Cursor cursor = db.query(IT_sql.CATEGORY_TBL, null, null, null, null, null, null);
		startManagingCursor(cursor);
		Log.d("TA", "getColumn = " + cursor.getColumnCount());
		return cursor;
	}
	
	public void showCategory(Cursor cursor){		
		while(cursor.moveToNext()){
			String text = cursor.getString(1);
			aListCategory.add(text);
			Log.d("TA", "category = " + text);
		}

	}
	
	//커서 어댑터
	class MyDBAdapter extends CursorAdapter{ 
		
		public MyDBAdapter(Context context, Cursor c) {
			super(context, c);			
		}

		@Override
		public void bindView(View V, Context context, Cursor C) {
			TextView tv = (TextView)V.findViewById(android.R.id.text1);			
			String text = C.getString(1);
			tv.setText(text);
		}		
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {			
			return getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);			
		}		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
}